Learning Perl by Randal L. Schwartz & brian d foy & Tom Phoenix

Learning Perl by Randal L. Schwartz & brian d foy & Tom Phoenix

Author:Randal L. Schwartz & brian d foy & Tom Phoenix
Language: eng
Format: mobi, epub
Tags: COMPUTERS / Programming Languages / Perl
Publisher: O'Reilly Media
Published: 2011-06-16T16:00:00+00:00


And this loop counts from –150 to 1000 by threes:[269]

for ($i = –150; $i <= 1000; $i += 3) {

print "$i\n";

}

In fact, you could make any of the three control parts (initialization, test, or increment) empty, if you wish, but you still need the two semicolons. In this (quite unusual) example, the test is a substitution, and the increment is empty:

for ($_ = "bedrock"; s/(.)//; ) { # loops while the s/// is successful

print "One character is: $1\n";

}

The test expression (in the implied while loop) is the substitution, which returns a true value if it succeeded. In this case, the first time through the loop, the substitution removes the b from bedrock. Each iteration removes another letter. When the string is empty, the substitution will fail, and the loop is done.

If the test expression (the one between the two semicolons) is empty, it’s automatically true, making an infinite loop. But don’t make an infinite loop like this until you see how to break out of such a loop, which we’ll show later in this chapter:

for (;;) {

print "It's an infinite loop!\n";

}



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.